package com.gfm.story.service.user;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gfm.story.dao.UserDao;
import com.gfm.story.entity.user.User;
import com.gfm.story.interceptor.UserContext;
import com.gfm.story.util.HashUtils;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @BelongsProject: story
 * @BelongsPackage: com.gfm.story.service.user
 * @Author: Administrator
 * @CreateTime: 2019-06-03 11:26
 * @Description: ${Description}
 */
@Service
public class UserService {
    @Resource
    private UserDao userDao;

    public boolean addUser(User user){
        user.setPassword(user.getPassword());//调用md5加密工具
        user.setIsDelete(0);
        if (user.insert()){
            return true;
        }
        return false;
    }

    public User getUser(String username ,String password) {
        EntityWrapper<User> wrapper = new EntityWrapper<User>();
        wrapper.eq("username", username);
        wrapper.eq("password", password);
        List<User> list =userDao.selectList(wrapper);
        if (!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    public boolean updatePwd(String username, String password, String cifmPassword, String email) {
        User user = new User();
        user.setUsername(username);
        user.setEmail(email);
        User user1 = userDao.selectOne(user);
        if (user1==null){
            return false;
        }else{
            if (user1.getPassword()!=password){
                return false;
            }
            user1.setPassword(cifmPassword);
            if (user1.updateById()){
                UserContext.setUser(user1);//修改session中的user
                return true;
            }
            return false;
        }
    }

    public User getUserBySession(User user){
        return userDao.selectOne(user);
    };

    public boolean IsNotUserName(User user) {
        User user1 = new User();
        user1.setEmail(user.getEmail());
        User user2 = userDao.selectOne(user1);
        if (user2==null){
            return true;
        }else{
            return false;
        }
    }

}
